
clear
set more off

/*********************************************************************************
Name: patients_health.do

Data In: [Data/Original/patient_surveys_attempts.dta,
		  Data/Original/patient_surveys.dta,
		  Data/Intermediate/patient_controls.dta,
		  Data/Original/hw_roster.dta,
		  Data/Intermediate/verified_patients.dta]

Data Out: []

Results Out: [Results/Appendix/TableA7_PanelA.out,
			  Results/Appendix/TableA7_PanelB.out]

Purpose of do-file: Estimating the treatment's impact on patients' health
*********************************************************************************/

* Setting path directory
cd "${DIRECTORY}"


** Calling and merging datasets

use "Data/Original/patient_surveys_attempts.dta", clear

merge 1:1 q1_ques_code a02_entry_exit using "Data/Original/patient_surveys.dta"
drop _merge

merge m:1 q1_ques_code using "Data/Intermediate/patient_controls.dta"
drop _merge

merge m:1 Unique_ID UID_Center using "Data/Original/hw_roster.dta"
keep if _merge == 3
drop _merge

merge 1:1 q1_ques_code a02_entry_exit using "Data/Intermediate/verified_patients.dta"
keep if _merge == 3
drop _merge


** Creating variables

* Declaring patient controls 

global patient_controls = "pat_male pat_age pat_caste_general pat_hindu pat_rw_both pat_ownhouse pat_hhd_size pat_migrate_always pat_migrate_6plus pat_time_to_center pat_dum_male pat_dum_age pat_dum_caste_general pat_dum_hindu pat_dum_rw_both pat_dum_ownhouse pat_dum_hhd_size pat_dum_migrate_always pat_dum_migrate_6plus pat_dum_time_to_center"

* Defining treatment outcome cured
gen cured = (j11_think_you_cure == 1 & (j12_nolonger_tb_1 == 1 | j12_nolonger_tb_2 == 2 | j12_nolonger_tb_3 == 3)) if j11_think_you_cure == 1 | j11_think_you_cure == 2

* Overall health
gen overall_health = e0_classify_health if e0_classify_health ~= -999

* Difficulty in doing tasks
foreach var in e1_water_well e2_routine_work e3_daily_activity e4_usual_work e5_usual_home_work{
	
	gen `var'_2 = (`var' == 1) if (`var' == 1 | `var' == 2 | `var' == 3 | `var' == 4)
	
	su `var'_2 if treatment == 0
	local control_mean = r(mean)
	local control_sd = r(sd)
	replace `var'_2 = (`var'_2 - `control_mean')/`control_sd'

	} 

egen nodifficulty_index = rowmean(e1_water_well_2 e2_routine_work_2 e3_daily_activity_2 e4_usual_work_2 e5_usual_home_work_2)

* General health symptoms, total
gen cough_total = (e8_grid_b == 1) if !missing(e8_grid_b)
gen resp_total = (e10_1_grid_b == 1) if !missing(e10_1_grid_b)
gen body_pain_total = (e10_2_grid_b == 1) if !missing(e10_2_grid_b)
gen gen_health_total = (e10_3_grid_b == 1) if !missing(e10_3_grid_b)
gen oth_health_total = (e10_4_grid_b == 1) if !missing(e10_4_grid_b)

foreach var in cough_total resp_total body_pain_total gen_health_total oth_health_total{
	su `var' if treatment == 0
	local control_mean = r(mean)
	local control_sd = r(sd)
	replace `var' = (`var' - `control_mean')/`control_sd'
} 
egen symptoms_index = rowmean(cough_total resp_total body_pain_total gen_health_total oth_health_total)

* General health symptoms, severity
// Following measures of increased symptoms or severity are only asked conditional on the earlier measures being positive. Hence, imputing zero where earlier measures are negative.
gen cough_severe_total = (e9_grid_b_1 == 1 | e9_grid_b_2 == 2 | e9_grid_b_3 == 3) if (e9_grid_b_1 == 1 | e9_grid_b_2 == 2 | e9_grid_b_3 == 3 | e9_grid_b_4 == 4) | e8_grid_b == 2
gen resp_severe_total = (e12_1_grid_b == 1) if !missing(e12_1_grid_b) | e10_1_grid_b == 2 
gen body_pain_severe_total = (e12_2_grid_b == 1) if !missing(e12_2_grid_b) | e10_2_grid_b == 2
gen gen_health_severe_total = (e12_3_grid_b == 1) if !missing(e12_3_grid_b) | e10_3_grid_b == 2
gen oth_health_severe_total = (e12_4_grid_b == 1) if !missing(e12_4_grid_b) | e10_4_grid_b == 2

foreach var in cough_severe_total resp_severe_total body_pain_severe_total gen_health_severe_total oth_health_severe_total{
	su `var' if treatment == 0
	local control_mean = r(mean)
	local control_sd = r(sd)
	replace `var' = (`var' - `control_mean')/`control_sd'
} 

egen symptoms_severe_index = rowmean(cough_severe_total resp_severe_total body_pain_severe_total gen_health_severe_total oth_health_severe_total)

* General health symptoms, increased
gen resp_increased_total = (e11_1_grid_b == 3) if !missing(e11_1_grid_b) | e10_1_grid_b == 2 
gen body_pain_increased_total = (e11_2_grid_b == 3) if !missing(e11_2_grid_b) | e10_2_grid_b == 2
gen gen_health_increased_total = (e11_3_grid_b == 3) if !missing(e11_3_grid_b) | e10_3_grid_b == 2
gen oth_health_increased_total = (e11_4_grid_b == 3) if !missing(e11_4_grid_b) | e10_4_grid_b == 2
foreach var in resp_increased_total body_pain_increased_total gen_health_increased_total oth_health_increased_total{

su `var' if treatment == 0
	local control_mean = r(mean)
	local control_sd = r(sd)
	replace `var' = (`var' - `control_mean')/`control_sd'
} 

egen symptoms_increased_index = rowmean(resp_increased_total body_pain_increased_total gen_health_increased_total oth_health_increased_total)

* Overall satisfaction/happiness

gen optimism = (o0_hh_situa_improve == 1 | o0_hh_situa_improve == 2) if (o0_hh_situa_improve == 1 | o0_hh_situa_improve == 2 | o0_hh_situa_improve == 3 | o0_hh_situa_improve == 4)
gen project = (o1_ongoing == 1) if (o1_ongoing == 1 | o1_ongoing == 2)
gen happy = (o17_overall_happy == 1 | o17_overall_happy == 2) if (o17_overall_happy == 1 | o17_overall_happy == 2 | o17_overall_happy == 3)
gen global_satisfaction = o18_global_satisfa if !missing(o18_global_satisfa) & o18_global_satisfa > 0

* Creating standard index of emotional health using measures for various emotional health indicators

foreach metric of varlist o5_hopeless o6_effort o7_worhless o8_relaxed o9_happy o10_envious o11_angry o12_unable_sleep o13_crying {
	
	* Replacing missing values by the mean in the corresponding group (treatment or control))
	gen `metric'_fill = `metric'
	replace `metric'_fill = . if `metric'_fill < 0
	
	* Standardising direction of variables and recoding to binary values. 1 = better health, 0 = worse health.
	if inlist("`metric'","o8_relaxed", "o9_happy") {
		
		recode `metric'_fill 1/2=1 3=0
	
	} 
	
	else {
		
		recode `metric'_fill 1/2=0 3=1
	
	}
	
	* Replacing missing values by the mean in the corresponding group (treatment or control))
	qui su `metric'_fill if treatment == 1
	local treat_mean = r(mean)
	di "`treat_mean'"
		
	qui su `metric'_fill if treatment == 0
	local control_mean = r(mean)
	local control_sd = r(sd)
	di "`control_mean'" "`control_sd'"
	
	* Standardizing each variable by control mean and SD
	
	replace `metric'_fill = (`metric'_fill - `control_mean')/`control_sd'

}

* Average all standardized variables
egen emo_health_index = rowmean(o5_hopeless_fill o6_effort_fill o7_worhless_fill o8_relaxed_fill o9_happy_fill o10_envious_fill o11_angry_fill o12_unable_sleep_fill o13_crying_fill)

* Impression of biometric system now
gen impression_sys_now = 5 - l3_impre_sys_now if !missing(l3_impre_sys_now)
replace impression_sys_now = 5 - l1_impre_sys_heard if l2_impre_sys_change == 2 & missing(l3_impre_sys_now) & !inlist(l1_impre_sys_heard,.,6) 
// Ignoring patients who answer "Does not remember" about old impression


** Descriptive statistics on impression of biometric system: reported in Section 6

label variable impression_sys_now "Impression of the biometric system"
ta impression_sys_now if verified_pat == 1 & treatment == 1
* 4: very good, 3: good, 2: indifferent, 1: bad, 0: very bad
* 62.8% say good or very good, 36.7% are indifferent, only 0.5% say bad or very bad


** Regressions

* Defining sample: all verified patients, and using their exit survey (range 2, 2.1, 3, 3.1)
gen sample_temp = (a02_entry_exit == 2 | a02_entry_exit == 3 | a02_entry_exit == 4 | a02_entry_exit == 5) & verified_pat == 1

* Analysing impact on patients' physical health: Table A7, Panel A

su cured if treatment == 0	& sample_temp == 1
local mean_control=r(mean)

ivreg2 cured treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA7_PanelA.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

foreach var in overall_health nodifficulty_index symptoms_index symptoms_severe_index symptoms_increased_index {

su `var' if treatment == 0 & sample_temp == 1
local mean_control=r(mean)
	
ivreg2 `var' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA7_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

}

* Analysing impact on patients' emotional health: Table A7, Panel B

su optimism if treatment == 0 & sample_temp == 1
local mean_control=r(mean)

ivreg2 optimism treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if sample_temp == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA7_PanelB.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

foreach var in project emo_health_index happy global_satisfaction {
	
	su `var' if treatment == 0 & sample_temp == 1
	local mean_control=r(mean)
	
	ivreg2 `var' treatment final_stratum_id1-final_stratum_id13 post_exp $patient_controls if sample_temp == 1, small cl(uid_cluster)   
	quietly outreg2 treatment using "Results/Appendix/TableA7_PanelB.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Patient controls, Yes) adec(3) dec(3) keep(treatment)

}